package cn.fresh.xuanyuan.module.trade.api.cart;

import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.module.trade.api.cart.dto.CartDto;
import cn.fresh.xuanyuan.module.trade.dal.dataobject.cart.CartDO;
import cn.fresh.xuanyuan.module.trade.dal.mysql.cart.CartMapper;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class CartApiImpl implements CartApi {

    @Resource
    private CartMapper cartMapper;
    @Override
    public CommonResult<List<CartDto>> getCurrentUserCartList(Long userId) {
        List<CartDto> list = new ArrayList<>();
        List<CartDO> carts = cartMapper.selectListByUserId(userId);
        if(CollectionUtil.isNotEmpty(carts)) {
            for(CartDO c : carts) {
                CartDto dto = new CartDto();
                BeanUtils.copyProperties(c, dto);
                list.add(dto);
            }
        }
        return CommonResult.success(list);
    }
}
